<?php
class Server{
	public static function add_server($name,$address,$pin){
		//log
		Log::add_log("Ajout d'un server",$name);
		$name = mysql_real_escape_string(htmlspecialchars($name));
		$address = mysql_real_escape_string(htmlspecialchars($address));
		$pin = mysql_real_escape_string(htmlspecialchars($pin));

		mysql_query("INSERT INTO ".DATABASE_TABLE_SERVER."(name,address,pin) VALUES('$name', '$address','$pin')");
	}
	
	public static function remove_server($box){
		$tablength = count($box);
		$i = 0;
		while ($i!=$tablength)
		{
			$sql_query=mysql_query("SELECT name FROM ".DATABASE_TABLE_SERVER." WHERE id = $box[$i] ORDER BY `name` ASC") or die(mysql_error());
			while ($query = mysql_fetch_array($sql_query) ){
				Log::add_log("remove d'un server",$query['name']);
				mysql_query("DELETE FROM ".DATABASE_TABLE_FILE." WHERE `server` = '".$query['name']."' ") or die(mysql_error());
			}
			mysql_query("DELETE FROM ".DATABASE_TABLE_SERVER." WHERE `id` = $box[$i]") or die(mysql_error());
			$i++;
		}
	}
	
	public static function check_serv_up($address){
		try {
			$address=Server::mise_en_forme_address($address);
			$time_start = microtime(TRUE);
			if( @fsockopen( $address, 80, &$errno, &$errstr, 1) ){
				$time_end = microtime(TRUE);
				$ping = round(($time_end - $time_start)*1000);
			}
			else{
				$ping = -1;
			}
		}catch (Exception $e){
			$ping = -1;
		}
		return $ping;
	}
	
	public static function refresh_server_old($server){
		//log
		Log::add_log("refresh d'une server",$server);
		//fonction
		$server=mysql_real_escape_string(htmlspecialchars($server));
		
			if ($server == "%"){
				$sql_query=mysql_query("Select name from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
				while ($query2 = mysql_fetch_array($sql_query) ){
					$array=File::file_in_array($query2['name']);
					if (! $array){
						
					}
					else{
						$sql_query2=mysql_query("DELETE FROM ".DATABASE_TABLE_FILE." WHERE server=\"".$query2['name']."\" ");
						for ($i=0;$i<count($array);$i++){
							$sql_query2=mysql_query("INSERT INTO ".DATABASE_TABLE_FILE."(name,taille,date,server,type,fullpath) VALUES('".mysql_real_escape_string($array[$i][0])."', '".mysql_real_escape_string($array[$i][1])."','".mysql_real_escape_string($array[$i][2])."','".mysql_real_escape_string($query2['name'])."','".mysql_real_escape_string($array[$i][4])."','".mysql_real_escape_string($array[$i][3])."')");
						}
						mysql_query("UPDATE ".DATABASE_TABLE_SERVER." SET last_update = '".time()."' WHERE name like '".$query2['name']."'");
					}
				}
			}
			else{
				$array=File::file_in_array($server);
				if (! $array){
					exit();
				}
				$sql_query=mysql_query("DELETE FROM ".DATABASE_TABLE_FILE." WHERE server=\"".$server."\" ");
				for ($i=0;$i<count($array);$i++){
					$sql_query=mysql_query("INSERT INTO ".DATABASE_TABLE_FILE."(name,taille,date,server,type,fullpath) VALUES('".mysql_real_escape_string($array[$i][0])."', '".mysql_real_escape_string($array[$i][1])."','".mysql_real_escape_string($array[$i][2])."','".mysql_real_escape_string($server)."','".mysql_real_escape_string($array[$i][4])."','".mysql_real_escape_string($array[$i][3])."')") or die(mysql_error());
					$sql_query=mysql_query("UPDATE ".DATABASE_TABLE_SERVER." SET last_update = '".time()."' WHERE name like '".mysql_real_escape_string($server)."'")  or die ('Erreur : '.mysql_error());
				}
			}
			exit();
	}
	
	public static function refresh_server($server){
		
		Log::add_log("resfresh d'un server",$server);
		$array_serv = array();
		if ($server == "%"){
			$sql_query=mysql_query("Select name from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
				while ($query2 = mysql_fetch_array($sql_query) ){
					$array_serv[] = $query2['name'];
				}
		}else{
			$server=mysql_real_escape_string(htmlspecialchars($server));
			$array_serv[] = $server;
		}
		foreach ($array_serv as $name){
			$sql_query = mysql_query("SELECT last_update FROM ".DATABASE_TABLE_SERVER." WHERE name=\"".$name."\" ");
			$query = mysql_fetch_array($sql_query);
			if ( (time()-$query['last_update'])>=60 ){
				$array=File::file_in_array($name);
				if ($array != false){		
					$sql_query2=mysql_query("DELETE FROM ".DATABASE_TABLE_FILE." WHERE server=\"".$name."\" ");
					for ($i=0;$i<count($array);$i++){
						$sql_query2=mysql_query("INSERT INTO ".DATABASE_TABLE_FILE."(name,taille,date,server,type,fullpath) VALUES('".mysql_real_escape_string($array[$i][0])."', '".mysql_real_escape_string($array[$i][1])."','".mysql_real_escape_string($array[$i][2])."','".mysql_real_escape_string($name)."','".mysql_real_escape_string($array[$i][4])."','".mysql_real_escape_string($array[$i][3])."')");
					}
					mysql_query("UPDATE ".DATABASE_TABLE_SERVER." SET last_update = '".time()."' WHERE name like '".$name."'");
				}
			}
		}	
	}
	
	
	public static function mise_en_forme_address($address){
		if (substr($address,0,7)=="http://"){
				$address=substr($address,7);
			}
		if (strpos($address,"/") != false){
			$address = substr($address,0,strpos($address,"/"));
		}
		return $address;
	}
}
?>